// pages/sort/sort.js
const app = getApp()
const util = require('../../utils/util.js');
const api = require('../../utils/api.js');
Page({
    data: {
        pageColor: app.pageColor,
        dataList:[],
        swiperTitle: [],
        menu: [],
        list:[],
        currentPage: 0,
        selected: -1,
        scrollLeft: 0, //tab标题的滚动条位置
        cate_id:null
    },
    turnTitle: function (e) {
        var that =this;
        this.setData({
            currentPage: e.detail.current,
            menu: [],
            list: [],
            selected: -1,
        },()=>{
            this.ajax();
        });
        
    },
    turnPage: function (e) {
        var that = this;
        var cur = e.currentTarget.dataset.index;
        if (this.data.currentPage == cur) { return false; }
        else {
            this.setData({
                currentPage: cur,
                menu: [],
                list: [],
                selected: -1,
            })
            that.ajax();
        }
    },
    turnMenu: function (e) {
        var that =this;
        that.setData({
            selected: e.currentTarget.dataset.index,
            cate_id: e.currentTarget.dataset.cateid
        })
        that.goodList();
    },
    search: function () {
        wx.navigateTo({
            url: '../search/search'
        })
    },
    onLoad: function (options) {
        var that = this;
        that.ajax();
    },
    onReady:function(){
        util.setNavigationBarTitle("分类");
    },
    ajax:function(){
        var that =this;
        let url = api.goodCate();
        util.http(false, "GET", url, "null", res => {
            that.setData({
                dataList:res,
                list: res.cates[that.data.currentPage].good_list
            })
        })
    },
    goodList:function(){
        var that =this;
        let careId = that.data.cate_id;
        let url = api.goodList();
        util.http(false,"GET", url, { cate_id: careId },res => {
            that.setData({
                list: res.good_list.data
            })
        })
    }
})